rm(list = ls()); library(foreign); library(ggplot2); library(grid); library(gridExtra)
library(ggalt); library(tidyverse); library(lubridate); library(diagram); library(readstata13); library(tidyr); library(dplyr); library(stargazer)

### ==== FIGURE 1 ==== ###
  
  df <- read.dta13("~/cross_section_mean.dta")
  
  df <- df %>%
    drop_na(demrep) %>%
    mutate(
      
      color = case_when(demrep==1~"red",
                        demrep==0~"skyblue"), 
      
      pch = 16, 
      
      outcome = case_when(
        
        outcome=="doctor_trust01"~"Trust Personal Doctor",
        outcome=="adherence01" ~ "Follow Personal\nDoc's Advice?",
        outcome=="med_conf01" ~ "Confidence in Medicine"
        
      ),
      
      ub = mean + se*1.96,
      lb = mean - se*1.96
      
      
    )
  
  df <- transform(df,
                  outcome=factor(outcome,
                                 levels=c("Confidence in Medicine","Trust Personal Doctor","Follow Personal\nDoc's Advice?")))
  
  
  

  ggplot(df, aes(x=year, y=mean, group=demrep, color=factor(demrep))) + 
    geom_errorbar(aes(ymin=lb, ymax=ub), width=0) +
    geom_point(size=5) + 
    geom_line(size=2) + 
    facet_wrap(~outcome) + 
    theme_bw() +
    theme(axis.title.x = element_text(size = 18),
          axis.title.y = element_text(size = 18),
          strip.text = element_text(size=20),
          axis.text.x = element_text(size = 18, angle = 90, color="black"),
          axis.text.y = element_text(size = 18, color = "black"),
          legend.title = element_blank(),
          legend.text = element_text(size=16)) + 
    scale_x_continuous(breaks = c(2011,2013,2019,2022), labels=c("2011","2013","2019","2022")) +
    scale_color_manual(values=c("skyblue", "coral"), labels=c("Democrat", "Republican")) + 
    labs(y="", x="Year", title="") +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1)) 

  
  
  ### ==== FIGURE 2 ==== ###
  
  
  df <- read.dta13("~/gss_institutions.dta")
  
  df <- mutate(df, outcome = case_when(outcome=="conarmy"~"Military",
                                       outcome=="conclerg"~"Organized Religion",
                                       outcome=="conlabor"~"Organized Labor",
                                       outcome=="conpress"~"Press",
                                       outcome=="consci"~"Scientific Community",
                                       outcome=="coneduc"~"Education",
                                       outcome=="contv"~"Television",
                                       outcome=="conbus"~"Major Companies",
                                       outcome=="conmedic"~"Medicine",
                                       outcome=="conjudge"~"Supreme Court",
                                       outcome=="conlegis"~'Congress',
                                       outcome=="confed"~"Exec Branch Fed. Govt"))
  
  ggplot(data = filter(df, party %in% 0:1 & outcome %in% c("Medicine", "Education", "Scientific Community")), aes(x=year, y=coef, group=party, color=factor(party))) + 
    geom_errorbar(aes(ymin=lb, ymax=ub), width=0) +
    geom_point(size=2) + 
    geom_line(size=1) + 
    facet_wrap(~outcome) + 
    theme_bw() +
    theme(axis.title.x = element_text(size = 18),
          axis.title.y = element_text(size = 18),
          strip.text = element_text(size=16),
          axis.text.x = element_text(size = 16, angle = 0, color="black"),
          axis.text.y = element_text(size = 14, color = "black"),
          legend.title = element_blank(),
          legend.text = element_text(size=16),
          legend.position="bottom") + 
    scale_color_manual(values=c("skyblue", "coral"), labels=c("Democrat", "Republican")) + 
    labs(y="", x="Year", title="")
  
  
  
  ### ==== FIGURE 3 ==== ###
  
  df <- read.dta13("~/experiment_results.dta")
  
  
  df <- df %>%
    mutate(index = case_when(
      votetrump==0 & var=="personaltrust"~.95,
      votetrump==1 & var=="personaltrust"~1.05,
      votetrump==99 & var=="personaltrust"~1.15,
      
      votetrump==0 & var=="adherence"~1.95,
      votetrump==1 & var=="adherence"~2.05,
      votetrump==99 & var=="adherence"~2.15,
      
      votetrump==0 & var=="confidence"~2.95,
      votetrump==1 & var=="confidence"~3.05,
      votetrump==99 & var=="confidence"~3.15))
  
  

  ggplot(data=df, aes(x=index, y=coef, group = factor(votetrump), color=factor(votetrump))) +
    geom_point(size=5) +
    geom_errorbar(aes(ymin=lb, ymax=ub), width=0) +
    geom_point() + 
    scale_x_continuous(breaks = c(1,2,3), labels=c("Trust Own\nDoctor","Adhere Doc\nAdvice","Conf. in\nMedicine")) +
    scale_color_manual(values=c("skyblue", "coral","gray60"), labels=c("Vote Biden", "Vote Trump", "Biden-Trump")) + 
    labs(y="Treatment - Control", x="", title="") +
    theme_bw() +
    theme(axis.title.y = element_text(size = 18),
          strip.text = element_text(size=18),
          axis.text.x = element_text(size = 18, color="black"),
          axis.text.y = element_text(size = 18, color="black"),
          legend.title = element_blank(),
          legend.text = element_text(size=16)) +
    geom_hline(yintercept=0, linetype="dashed")

  
  
  
  ### ==== FIGURE 4 ==== ###
  
  
  df <- read.dta13("~/conjoint_results_all.dta")
  
  measure <- df$measure[1:8]
  measure.label <- c("Male", "Ivy League", "Far Away", "Democrat", "High Rating", "Medium Rating", "Black", "Hispanic")
  measure.label <- rev(measure.label)
  
  df <- df %>%
    mutate(votetrump = case_when(votetrump==1~"Trump Voters",
                                 votetrump==0~"Biden Voters"),
           
           
           color = case_when(votetrump=="Trump Voters"~"coral",
                             votetrump=="Biden Voters"~"skyblue"),
           
           index = case_when(votetrump=="Trump Voters"~index+.15,
                             votetrump=="Biden Voters"~index-.15),
           
           keep = case_when(votetrump=="Trump Voters" & group == "black" ~ 1, 
                            votetrump=="Biden Voters" ~ 2,
                            votetrump=="Trump Voters" & group != "black"~2), 
           
           group = case_when(subset=="blackx" ~ "Black Respondents",
                             subset=="hispanicx" ~ "Latinx Respondents", 
                             subset=="femalex" ~ "Female Respondents",
                             subset=="all" ~ "All Respondents"),
           
           keep = case_when(subset=="blackx" & votetrump=="Trump Voters"~1)
           
    ) %>%
    
    subset(is.na(keep)) %>%
    subset(pref=="repdem") %>%
    transform(group=factor(group,levels=c("All Respondents", "Female Respondents","Black Respondents","Latinx Respondents")))
  

  ggplot(data=df, aes(x=coef, y=index, group=factor(votetrump), color=factor(votetrump))) +
    geom_point(size=5) +
    geom_errorbar(aes(xmin=lb, xmax=ub), width=0) +
    geom_vline(xintercept = 0, linetype="dashed", linewidth=1) +
    scale_y_continuous(breaks = c(1:8), labels=c(measure.label)) +
    labs(y="", x="Marginal Effect", title="") + 
    facet_wrap(~group) +
    theme_bw() +
    scale_color_manual(values=c("skyblue", "coral"), labels=c("Democrat", "Republican")) + 
    labs(y="", x="AMCE", title="") +
    theme(axis.title.y = element_text(size = 18),
          axis.title.x = element_text(size = 18),
          strip.text = element_text(size=24),
          axis.text.x = element_text(size = 22, color="black"),
          axis.text.y = element_text(size = 20, color="black"),
          legend.title = element_blank(),
          legend.text = element_text(size=16))

  
  ##### TABLE 2 & 3 #######
  
  df <- read.dta13("~/zocdoc_bjps.dta")

  beta.seek <- NA; alpha.seek <- NA; pvalue.seek <- NA; obs.seek <- NA
  beta.email <- NA; alpha.email <- NA; pvalue.email <- NA; obs.email <- NA
  
  for(i in c(1:7)){
    
    beta.seek[i] <- round(summary(lm(seek~treat01, data=filter(df, ideology==i)))$coef[2],3)
    pvalue.seek[i] <- round(summary(lm(seek~treat01, data=filter(df, ideology==i)))$coef[2,4],3)
    alpha.seek[i] <- round(summary(lm(seek~treat01, data=filter(df, ideology==i)))$coef[1],3)
    obs.seek[i] <- round(summary(lm(seek~treat01, data=filter(df, ideology==i)))$df[2] + 2,3)
    
    beta.email[i] <- round(summary(lm(email~treat01, data=filter(df, ideology==i)))$coef[2],3)
    pvalue.email[i] <- round(summary(lm(email~treat01, data=filter(df, ideology==i)))$coef[2,4],3)
    alpha.email[i] <- round(summary(lm(email~treat01, data=filter(df, ideology==i)))$coef[1],3)
    obs.email[i] <- round(summary(lm(email~treat01, data=filter(df, ideology==i)))$df[2] + 2,3)
    
    
  }
  
  # MAKE TREATMENT AVG
  cons.seek <- alpha.seek + beta.seek
  cons.email <- alpha.email + beta.email
  
  # MAKE COLUMN LABELS
  ideology.label <- c("Extremely Liberal", "Liberal", "Slightly Liberal", "Moderate", "Slightly Conservative", "Conservative", "Extremely Conservative")
  
  # MAKE SEEK TABLE
  seek.df <- cbind.data.frame(ideology.label, cons.seek, alpha.seek, beta.seek, pvalue.seek, obs.seek)
  names(seek.df) <- c("Ideology", "Cons Prof", "ZocDoc",  "Difference", "P-value Diff", "Obs.")
  stargazer(seek.df, summary=F, rownames=F, float= F)
  
  
  # MAKE EMAIL TABLE
  email.df <- cbind.data.frame(ideology.label, cons.email, alpha.email, beta.email, pvalue.email, obs.email)
  names(email.df) <- c("Ideology", "Cons Prof", "ZocDoc",  "Difference", "P-value Diff", "Obs.")
  stargazer(email.df, summary=F, rownames=F, float= F)
  
  
  
  
  
  
